@file:Suppress("SpellCheckingInspection")

package han.cirno.corrupt

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import han.cirno.corrupt.data.instances.UserClassesData
import han.cirno.corrupt.components.classes.ToggleList
import han.cirno.corrupt.util.RegexUtil
import han.cirno.corrupt.util.StreamUtil
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.TextNode
import org.junit.Test

class ClassTest {
    @Test
    fun xmlAnalyseTest(){
        val context = ApplicationProvider.getApplicationContext<Context>()
        var array:ByteArray
        context.assets.open("test.html").use { array=StreamUtil.readToByteArray(it) }
        val document = Jsoup.parse(String(array))
        val table = document.getElementById("Table1")?:throw Exception()
        val children = table.children()[0].children()
        val classTable = UserClassesData.ClassTable(findToggleList(document,"xnd")!!.getSelected(),"1")
        children.withIndex().forEach { (i,child)->
            if(i<2)return@forEach
            val classNum=i-2
            var found=false
            var day=0
            child.children().forEach Inner@{
                if(RegexUtil.matchFirst("(第).*?(节)",it.text())!=null&&!found){
                    found=true
                    return@Inner
                }
                if (!found)return@Inner
                var rowSpan = it.attribute("rowspan")?.value?.toInt()
                if(rowSpan==null)rowSpan=1
                it.ownText()
                classTable.classEntries.addAll(genEntry(it.textNodes(),classNum,rowSpan,day))
                day++
            }
        }
        classTable.classEntries.forEach {
            it.run {
                println("$name $teachers $dow ${weeks.contentToString()} $time $place")
            }
        }
    }

    private fun findToggleList(document: Document,id:String):ToggleList<String>?{
        val element = document.getElementById(id)?:return null
        val list:MutableList<String> = ArrayList()
        var selectedIndex=-1
        element.children().withIndex().forEach {(i,it)->
            val value= it.attribute("value")?.value ?: return@forEach
            list.add(value)
            if(it.attribute("selected")?.value=="selected")selectedIndex=i
        }
        return ToggleList(list,selectedIndex)
    }

    private fun genEntry(content: MutableList<TextNode>, row:Int, rowSpan:Int, day:Int):MutableList<UserClassesData.ClassEntry>{
        val strings:ArrayList<String> =ArrayList()
        content.forEach { strings.add(it.text()) }
        val output:MutableList<UserClassesData.ClassEntry> =ArrayList()
        strings.withIndex().forEach {(i,s)->
            val weeks = RegexUtil.matchFirst("(?<=\\{).*?(?=\\})", s) ?: return@forEach
            val name=strings[i-1]
            val teacher=strings[i+1]
            val place=strings[i+2]
            val weeksSplit = weeks.split("|")
            val weeksList:MutableList<Int> =ArrayList()
            weeksSplit.forEach {
                val rangeMatch = RegexUtil.matchFirst("(?<=第).*?(?=周)", it)
                if(rangeMatch!=null){
                    val fromToWeeks = rangeMatch.split("-")
                    weeksList.addAll(IntRange(fromToWeeks[0].toInt(),fromToWeeks[1].toInt()))
                }
                val iterator = weeksList.iterator()
                if(it=="单周")
                    for (week in iterator)
                        if(week%2==0)iterator.remove()
                if (it=="双周")
                    for (week in iterator)
                        if(week%2!=0)iterator.remove()
            }
            //output.add(UserClassesData.ClassEntry(name,teacher, UserClassesData.DayOfWeek.valueOf(day)!!,
            //    weeksList.toIntArray(), UserClassesData.TimeRange(1, 2),place))
        }
        return output
    }
}
